package top.appx.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import top.appx.entity.ArticleEntity;
import top.appx.entity.FolderEntity;
import top.appx.qo.ArticleQO;
import top.appx.service.ArticleService;
import top.appx.service.FolderService;
import top.appx.vo.ArticleVO;
import top.appx.vo.FolderVO;
import top.appx.vo.Pager;
import top.appx.vo.ResultVO;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.util.List;

/**
 * Created by zwyhome on 2015/5/28.
 */
@Controller
public class ArticleController {

    @Resource
    private ArticleService articleService;
    @Resource
    private FolderService folderService;



    @RequestMapping(value= "article/controller",params ="action=queryByPage")
    @ResponseBody
    public  Object queryByPage(ArticleQO article,int page,int rows){
        Pager<ArticleVO> pager =  articleService.queryByPage(article);
        return pager;
    }

    @RequestMapping(value="article/controller",params = "action=save")
    @ResponseBody
    public ResultVO save(ArticleEntity article){
        articleService.save(article);
        ResultVO rv = ResultVO.success();
        rv.put("id",article.getId());
        return rv;
    }

    @RequestMapping(value="article/controller",params = "action=update")
    @ResponseBody
    public ResultVO update(ArticleEntity article){
        articleService.update(article);
        return ResultVO.success();
    }

    @RequestMapping(value="article/controller",params = "action=delById")
    @ResponseBody
    public ResultVO delById(int id){
        articleService.delById(id);
        return ResultVO.success();
    }
    @RequestMapping(value = "article/{id}")
    public String article(@PathVariable("id") int id,HttpServletRequest request){
        ArticleVO av = articleService.getById(id);
        request.setAttribute("av", av);
        return "article";
    }
    @RequestMapping(value="article_add")
    public String article_add(){
        return "article_add";
    }


    @RequestMapping(value="article/list")
    public String list(Integer fid,ArticleQO articleQO,HttpServletRequest request) throws UnsupportedEncodingException {
        if(articleQO.getTitle()!=null) {
            articleQO.setTitle(new String(articleQO.getTitle().getBytes("iso-8859-1"), "utf-8"));
        }
        if(articleQO.getPage()<1){
            articleQO.setPage(1);
        }
        if(articleQO.getPageSize()<1){
            articleQO.setPageSize(20);
        }
        request.setAttribute("articleQO",articleQO);

        if(fid!=null){
            FolderEntity f = folderService.getById(fid);
            request.setAttribute("folder",f);
        }
        Pager<ArticleVO> articleVOPager = articleService.queryByPage(articleQO);
        request.setAttribute("articleVOPager",articleVOPager);

        List<FolderVO> folderList = folderService.queryAll();
        request.setAttribute("folderList",folderList);

        return "list";
    }

}
